package com.springboot.test.common.exception;

import com.springboot.test.common.enums.ErrorCode;
import com.springboot.test.common.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

//异常捕获
@RestControllerAdvice
@Slf4j
public class ExceptionCatchController {
    /**
     * 全局捕获自定义异常
     * @param exception
     * @return
     */
    @ExceptionHandler(BizException.class)
    public Response bizExceptionCatch(BizException exception, HttpServletRequest httpServletRequest){
        log.error("调用地址:{},错误信息:{}", httpServletRequest.getRequestURI(),exception);
        return Response.error(exception.getCode(),exception.getMsg());
    }

    /**
     * 全局捕获异常，系统故障统一返回‘系统异常’提示
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Response exceptionCatch(Exception e, HttpServletRequest httpServletRequest){
        log.error("调用地址:{},错误信息:{}", httpServletRequest.getRequestURI(),e);
        return Response.error(ErrorCode.ERROR);
    }
}
